﻿using Lamex.Base;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lamex.mSchedule.Common
{
    public class ScanRecordHelper
    {
        /// <summary>
        /// 插入扫描记录表
        /// </summary>
        /// <param name="NOID"></param>
        /// <param name="BARCODE"></param>
        /// <param name="SCAN_QTY"></param>
        /// <param name="TIME_LENGTH"></param>
        /// <param name="SCAN_TIME"></param>
        /// <param name="SCANNER_NO"></param>
        /// <param name="LOCATION_CODE"></param>
        /// <param name="STATION_NAME"></param>
        public static void Insert(string NOID, string BARCODE, int SCAN_QTY, double TIME_LENGTH, DateTime SCAN_TIME, string SCANNER_NO, string LOCATION_CODE, string STATION_NAME, string BARCODE_TYPE)
        {
            string sql_Insert = "";
            sql_Insert = @"insert into [LamexMESManager].[dbo].[HR_Scan_Record] ([NOID]
      ,[BARCODE]
      ,[SCAN_QTY]
      ,[TIME_LENGTH]
      ,[SCAN_TIME]
      ,[SCANNER_NO]
      ,[LOCATION_CODE]
      ,[STATION_NAME]
      ,[BARCODE_TYPE])
	  values('" + NOID + "','" + BARCODE + "'," + SCAN_QTY + "," + TIME_LENGTH + ",'" + SCAN_TIME + "','" + SCANNER_NO + "','" + LOCATION_CODE + "','" + STATION_NAME + "','" + BARCODE_TYPE + "')";
            BaseBuildSql.ExecuteNonQuery(sql_Insert);
        }
        /// <summary>
        /// 插入非法条码Log
        /// </summary>
        /// <param name="BARCODE"></param>
        /// <param name="BARCODE_DESC"></param>
        /// <param name="STATION_NAME"></param>
        /// <param name="SCAN_TIME"></param>
        public static void Insert_Error(string BARCODE, string BARCODE_DESC, string STATION_NAME, DateTime SCAN_TIME)
        {
            string sql_Insert_Error = @"insert into [LamexMESManager].[dbo].[HR_Scan_Error_Log] ([BARCODE]
      ,[BARCODE_DESC]
      ,[STATION_NAME]
      ,[SCAN_TIME])
      values('" + BARCODE + "','" + BARCODE_DESC + "','" + STATION_NAME + "','" + SCAN_TIME + "')";
            BaseBuildSql.ExecuteNonQuery(sql_Insert_Error);
        }
        /// <summary>
        /// 写入显示gridcontrolUI
        /// </summary>
        /// <param name="dtUI"></param>
        /// <param name="BARCODE"></param>
        /// <param name="SCANNER_NO"></param>
        /// <param name="BARCODE_CN"></param>
        public static void Insert_GridControl(DataTable dtUI, string BARCODE, string SCANNER_NO, int SCAN_QTY, string LOCATION_CODE)
        {
            DataRow drUI = dtUI.NewRow();
            drUI["BARCODE"] = BARCODE;
            drUI["SCAN_TIME"] = DateTime.Now.ToString("HH:mm:ss");
            drUI["LOCATION_CODE"] = LOCATION_CODE;
            drUI["SCAN_QTY"] = SCAN_QTY;
            drUI["SCANNER_NO"] = SCANNER_NO;
            drUI["BARCODE_CN"] = "";
            dtUI.Rows.Add(drUI);
        }
        public static void Update(string BARCODE, string SCANTIME)
        {
            string sql_Update = @"update [LamexMESManager].[dbo].[HR_Scan_Record] set CANCEL_FLAG='1' where BARCODE='" + BARCODE + "'  and SCAN_TIME='" + SCANTIME + "'";
            BaseBuildSql.ExecuteNonQuery(sql_Update);
        }
        public static double SCAN_TIME_LENGTH(string firstTime, string lastTime, string STATION_NAME)
        {
            double m = 0;
            string sql = @"SELECT SUM(TIME_LENGTH) FROM [LamexMESManager].[dbo].[HR_Scan_Record] WHERE SCAN_TIME>(
SELECT TOP 1 SCAN_TIME FROM [LamexMESManager].[dbo].[HR_Scan_Record] WHERE BARCODE IN (
SELECT TOP 1 BARCODE FROM [LamexMESManager].[dbo].[HR_Scan_Record]
 WHERE (ISNULL(NOID,'')='' or NOID=0) and ISNULL(CANCEL_FLAG,'')!='1' and SCAN_TIME>'" + firstTime + "' and SCAN_TIME<'" + lastTime + "' and STATION_NAME='" + STATION_NAME + "' and (BARCODE like 'DK%' or BARCODE like 'PL%' or BARCODE like 'PN%' or BARCODE like 'MG%')   ORDER BY SCAN_TIME DESC) ORDER BY SCAN_TIME DESC)";
            DataTable dt = BaseBuildSql.GetDataTableBySql(sql);
            if (dt.Rows.Count > 0)
            {
                if (dt.Rows[0][0].ToString().Trim().Length > 0)
                {
                    m = Convert.ToDouble(dt.Rows[0][0].ToString());
                }
            }
            return m;
        }
    }
}
